//
//  LLWebImageActivityIndicator.m
//  SDWebExercise
//
//  Created by admin on 2019/9/7.
//  Copyright © 2019 admin. All rights reserved.
//

#import "LLWebImageActivityIndicator.h"

@interface LLWebImageActivityIndicator ()

// 遵守<SDWebImageIndicator>，必须要提供的一个indicatorView
@property (nonatomic, strong, nonnull) UIView *indicatorView;

@property (nonatomic, strong) UILabel *countLabel;
@property (nonatomic, strong) CAShapeLayer *sLayer;

@end

@implementation LLWebImageActivityIndicator

- (instancetype)init
{
    self = [super init];
    if (self) {
        _indicatorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 60, 60)];
        
        _countLabel = [[UILabel alloc] initWithFrame:_indicatorView.bounds];
        _countLabel.textColor = kMainColor;
        _countLabel.font = [UIFont systemFontOfSize:14];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        [_indicatorView addSubview:_countLabel];
        
        UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, 60, 60)];
        
        self.sLayer = [CAShapeLayer layer];
        self.sLayer.strokeColor = kMainColor.CGColor;
        self.sLayer.strokeStart = 0;
        self.sLayer.strokeEnd = 0;
        self.sLayer.lineWidth = 3;
        self.sLayer.lineCap = kCALineCapRound;
        self.sLayer.fillColor = [UIColor clearColor].CGColor;
        self.sLayer.path = path.CGPath;
        
        [_indicatorView.layer addSublayer:self.sLayer];
        
        
    }
    return self;
}


- (void)startAnimatingIndicator
{
//    self.indicatorView.backgroundColor = [UIColor whiteColor];
    self.indicatorView.hidden = NO;
}

- (void)stopAnimatingIndicator
{
//    self.indicatorView.backgroundColor = [UIColor whiteColor];
    self.indicatorView.hidden = YES;
}


- (void)updateIndicatorProgress:(double)progress
{
    self.sLayer.strokeEnd = progress;
    
    if (progress <= 0) {
        progress = 0;
    }
    self.countLabel.text = [NSString stringWithFormat:@"%.0f%%", progress * 100];
    
}


@end
